package cc.chinagps.gateway.client.packet.down;

import cc.chinagps.gateway.client.bean.TerminalType;
import cc.chinagps.gateway.client.packet.BasePacket;
import cc.chinagps.gateway.client.packet.base.PacketAdapter;
import cc.chinagps.gateway.util.CommonUtils;
import cc.chinagps.gateway.util.DateTimeUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.nio.charset.StandardCharsets;

@EqualsAndHashCode(callSuper = true)
@Data
public class Packet04 extends PacketAdapter {
    String logoutTime;
    short serialNo;
    byte status = 0;
    int longitude;
    int latitude;

    public Packet04(BasePacket basePacket) {
        super(basePacket);
    }

    public void decodeBusinessData(byte[] data) throws Exception {

    }

    @Override
    public void decodeBusinessData() throws Exception {
        byte[] data = basePacket.getMsgBody();
        decodeBusinessData(data);
    }

    @Override
    public byte[] encodeBussinessData() throws Exception {
        ByteBuf buf = Unpooled.buffer(16);
        TerminalType terminalType = TerminalType.find(getBasePacket().getDeviceInfo().getTerminalType());
        buf.writeBytes(logoutTime.getBytes(StandardCharsets.US_ASCII));
        buf.writeShort(serialNo);
        switch (terminalType) {
            case ELECTRONIC_LABEL:
                break;
            case OBD:
                buf.writeByte(status);
                break;
            default:
                throw new Exception("terminal type error,terminalType:" + terminalType);
        }
        buf.writeInt(longitude);
        buf.writeInt(latitude);
        return CommonUtils.getByteArray(buf);
    }
}
